<template>
  <div class="hotel-rooms">
    <div class="section-header">
      <home-outlined class="section-icon" />
      <h2>可预订客房</h2>
    </div>

    <div class="filters-card">
      <a-space :size="16" wrap>
        <div class="filter-item">
          <calendar-outlined class="filter-icon" />
          <a-range-picker 
            v-model:value="dateRange" 
            :disabled-date="disabledDate" 
            size="large"
            :bordered="false"
            class="date-picker" 
          />
        </div>
        <div class="filter-item">
          <team-outlined class="filter-icon" />
          <a-select 
            v-model:value="guestCount" 
            placeholder="选择入住人数" 
            style="width: 140px" 
            size="large"
            :bordered="false"
          >
            <a-select-option v-for="i in 4" :key="i" :value="i">{{ i }}人</a-select-option>
          </a-select>
        </div>
        <a-button type="primary" size="large" @click="handleSearch">
          <search-outlined />
          搜索
        </a-button>
      </a-space>
    </div>

    <a-spin :spinning="loading.rooms" tip="加载中...">
      <div v-if="rooms.length > 0" class="rooms-grid">
        <a-card v-for="room in rooms" :key="room.id" class="room-card" :bordered="false" hoverable>
          <div class="room-content">
            <div class="room-image-container">
              <img :src="room.image || room.imageUrl" :alt="room.name" class="main-image">
              <div class="image-gallery">
                <img v-for="(img, index) in (room.gallery || room.galleryUrls || []).slice(0, 3)" 
                     :key="index" 
                     :src="img" 
                     @click="showGallery(room.gallery || room.galleryUrls || [], index)"
                     class="gallery-thumbnail">
                <div v-if="(room.gallery || room.galleryUrls || []).length > 3" 
                     class="more-images" 
                     @click="showGallery(room.gallery || room.galleryUrls || [], 3)">
                  +{{ (room.gallery || room.galleryUrls || []).length - 3 }}
                </div>
              </div>
            </div>

            <div class="room-info">
              <div class="room-header">
                <h3>{{ room.name }}</h3>
                <div class="room-tags">
                  <a-tag v-for="tag in room.tags" :key="tag" color="blue">{{ tag }}</a-tag>
                </div>
              </div>

              <div class="room-features">
                <div class="feature">
                  <team-outlined />
                  <span>最多{{ room.capacity }}人</span>
                </div>
                <div class="feature">
                  <column-width-outlined />
                  <span>{{ room.size }}平方米</span>
                </div>
                <div class="feature">
                  <border-outlined />
                  <span>{{ room.bedType }}</span>
                </div>
              </div>

              <div class="room-amenities">
                <div v-for="(amenity, index) in processedAmenities(room)" 
                     :key="index" 
                     class="amenity">
                  <component :is="getAmenityIcon(amenity)" />
                  <span>{{ typeof amenity === 'string' ? amenity : amenity.name }}</span>
                </div>
              </div>

              <div class="room-footer">
                <div class="price-info">
                  <div class="price">¥{{ room.price }}</div>
                  <div class="per-night">每晚</div>
                </div>
                <a-button type="primary" size="large" @click="handleBook(room)" class="book-button">
                  立即预订
                </a-button>
              </div>
            </div>
          </div>
        </a-card>
      </div>
      <div v-else-if="!loading.rooms" class="empty-rooms">
        <a-empty description="暂无可用房间" />
      </div>
    </a-spin>

    <a-modal v-model:visible="galleryVisible" 
             :footer="null" 
             width="800px"
             class="gallery-modal">
      <a-image-preview-group>
        <a-image v-for="(img, index) in currentGallery"
                 :key="index"
                 :src="img"
                 :preview="{ visible: false }"
                 class="gallery-image" />
      </a-image-preview-group>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { Dayjs } from 'dayjs'
import dayjs from 'dayjs'
import { 
  TeamOutlined,
  ColumnWidthOutlined,
  BorderOutlined,
  WifiOutlined,
  ThunderboltOutlined,
  DesktopOutlined,
  CoffeeOutlined,
  SafetyCertificateOutlined,
  HomeOutlined,
  CalendarOutlined,
  SearchOutlined
} from '@ant-design/icons-vue'
import { useHotelDetailStore } from '@/stores/hotelDetailStrore'
import { storeToRefs } from 'pinia'
import type { HotelRoom } from '@/stores/hotelDetailStrore'

// 使用store获取房间数据
const hotelDetailStore = useHotelDetailStore()
const { hotelDetail, loading } = storeToRefs(hotelDetailStore)
const rooms = computed(() => hotelDetail.value.rooms)

const emit = defineEmits<{
  (e: 'roomSelect', room: HotelRoom): void
  (e: 'search', params: { dateRange: [Dayjs, Dayjs], guestCount: number }): void
}>()

const dateRange = ref<[Dayjs, Dayjs]>([dayjs(), dayjs().add(1, 'day')])
const guestCount = ref<number>(2)
const galleryVisible = ref(false)
const currentGallery = ref<string[]>([])

const disabledDate = (current: Dayjs) => {
  return current && current < dayjs().startOf('day')
}

const showGallery = (gallery: string[], startIndex: number) => {
  currentGallery.value = gallery
  galleryVisible.value = true
}

const handleBook = (room: HotelRoom) => {
  // 使用emit向父组件传递选择的房间信息
  emit('roomSelect', room)
}

const handleSearch = () => {
  emit('search', {
    dateRange: dateRange.value,
    guestCount: guestCount.value
  })
}

// 处理设施数据，兼容不同的数据格式
const processedAmenities = (room: HotelRoom) => {
  if (!room.amenities) return []
  
  // 如果是字符串数组，直接返回
  if (Array.isArray(room.amenities) && typeof room.amenities[0] === 'string') {
    return room.amenities
  }
  
  // 如果是对象数组，返回原样
  return room.amenities
}

// 根据设施名称获取对应的图标组件
const getAmenityIcon = (amenity: any) => {
  const amenityName = typeof amenity === 'string' ? amenity : amenity.name
  const iconMap: Record<string, any> = {
    'WiFi': WifiOutlined,
    '免费WiFi': WifiOutlined,
    '无线网络': WifiOutlined,
    '空调': ThunderboltOutlined,
    '电视': DesktopOutlined,
    '咖啡机': CoffeeOutlined,
    '保险箱': SafetyCertificateOutlined
  }
  
  return iconMap[amenityName] || DesktopOutlined
}
</script>

<style scoped>
.hotel-rooms {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.section-icon {
  font-size: 24px;
  color: #1890ff;
}

h2 {
  font-size: 20px;
  margin: 0;
  color: #262626;
  font-weight: 600;
}

.filters-card {
  margin-bottom: 24px;
  padding: 16px 24px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.06);
}

.filter-item {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  padding: 4px 12px;
  border-radius: 8px;
}

.filter-icon {
  color: #1890ff;
  margin-right: 8px;
  font-size: 16px;
}

.rooms-grid {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.room-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0,0,0,0.06);
  transition: all 0.3s ease;
}

.room-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.1);
  transform: translateY(-2px);
}

.room-content {
  display: flex;
  gap: 24px;
}

.room-image-container {
  width: 300px;
  flex-shrink: 0;
}

.main-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.main-image:hover {
  transform: scale(1.02);
}

.image-gallery {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.gallery-thumbnail {
  width: 60px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.gallery-thumbnail:hover {
  opacity: 0.8;
}

.more-images {
  width: 60px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}

.room-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.room-header {
  margin-bottom: 16px;
}

.room-header h3 {
  font-size: 20px;
  margin: 0 0 8px 0;
  color: #262626;
  font-weight: 600;
}

.room-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.room-features {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  margin-bottom: 16px;
}

.feature {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #595959;
}

.feature .anticon {
  color: #1890ff;
}

.room-amenities {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.amenity {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #595959;
  padding: 8px;
  background: #f5f5f5;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.amenity:hover {
  background: #e6f7ff;
}

.amenity .anticon {
  color: #1890ff;
}

.room-footer {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price-info {
  text-align: right;
}

.price {
  font-size: 24px;
  font-weight: bold;
  color: #f5222d;
}

.per-night {
  color: #8c8c8c;
  font-size: 14px;
}

.book-button {
  min-width: 120px;
}

.gallery-modal {
  :deep(.ant-modal-content) {
    padding: 24px;
    background: #f0f2f5;
  }
}

.gallery-image {
  width: 100%;
  max-height: 500px;
  object-fit: contain;
  margin-bottom: 16px;
}

@media (max-width: 768px) {
  .room-content {
    flex-direction: column;
  }
  
  .room-image-container {
    width: 100%;
  }
  
  .main-image {
    height: 240px;
  }
  
  .room-features {
    gap: 16px;
  }
  
  .room-amenities {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }
}

.empty-rooms {
  padding: 40px;
  text-align: center;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.06);
}
</style> 